package com.shakespace.artofandroid.chapter03.basic

import android.os.Bundle
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.VelocityTracker
import android.view.ViewConfiguration
import androidx.appcompat.app.AppCompatActivity
import com.shakespace.artofandroid.R
import com.shakespace.firstlinecode.global.TAG
import kotlinx.android.synthetic.main.activity_view_coordinate.*

class ViewCoordinateActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_coordinate)


        val memo = """
            left = getLeft()
            right = getRight()
            top = getTop()
            bottom = getBottom()

            width = right - left
            height = bottom - top
        """.trimIndent()

        tv_coordinate.text = memo

        val memo2 = """
            when move (left ,top ,right ,bottom are the same):
            x = left + translationX
            y = top + translationY
        """.trimIndent()

        tv_coordinate_move.text = memo2

        // 2020-01-14 NOTE:  can found in frameworks/base/core/res/res/values/config.xml  --> config_viewConfigurationTouchSlop
        val scaledTouchSlop = ViewConfiguration.get(this).scaledTouchSlop  // 24

        val other = """
           scaledTouchSlop =  $scaledTouchSlop
        """.trimIndent()

        tv_memo.text = other

        // not work when enabled = false
        tv_disable.setOnTouchListener { _, _ ->
            Log.e(this.TAG, "onCreate: tv_diable ontouch")
            false

        }
        
        tv_disable.setOnClickListener {
            Log.e(this.TAG, "onCreate: tv_disable onClick")
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {

        // 2020-01-14 NOTE: velocity = (end - start) / interval
        val velocityTracker = VelocityTracker.obtain()
        when (event?.action) {
            MotionEvent.ACTION_MOVE -> {
                velocityTracker.computeCurrentVelocity(100)
                Log.e(this.TAG, "onTouchEvent: ${velocityTracker.xVelocity} --  ${velocityTracker.yVelocity}")


            }
        }

        velocityTracker.clear()
        velocityTracker.recycle()

        return super.onTouchEvent(event)
    }

    // NPE
//    private val gestureDetector = GestureDetector(this, object : GestureDetector.OnGestureListener {
//        // TOUCH_DOWN but not MOVE OR UP
//        override fun onShowPress(e: MotionEvent?) {
//        }
//
//        // TOUCH_DOWN and TOUCH_UP
//        override fun onSingleTapUp(e: MotionEvent?): Boolean {
//            return false
//        }
//
//        // one TOUCH_DOWN
//        override fun onDown(e: MotionEvent?): Boolean {
//            return false
//        }
//
//        // one TOUCH_DOWN and many TOUCH_MOVE and one TOUCH_UP
//        override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
//            return false
//        }
//
//        // one TOUCH_DOWN and  many TOUCH_MOVE
//        override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean {
//            return false
//        }
//
//        override fun onLongPress(e: MotionEvent?) {
//        }
//
//    })

    private val doubleTapListener = object : GestureDetector.OnDoubleTapListener {

        // onSingleTapConfirmed  and  onDoubleTap  are alternative
        override fun onDoubleTap(e: MotionEvent?): Boolean {
            return false
        }

        override fun onDoubleTapEvent(e: MotionEvent?): Boolean {
            return false
        }

        override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
            return false
        }

    }
}
